package com.apexedu.portal.controller;

import com.apexedu.framework.util.ConfigUtil;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.IdConstants;
import com.apexedu.identity.entity.TSysMenu;
import com.apexedu.identity.entity.TSysUser;
import com.apexedu.portal.constant.ActionConstant;
import com.apexedu.portal.constant.Constants;
import com.apexedu.portal.entity.*;
import com.apexedu.portal.util.LittleTimer;
import com.apexedu.util.DateUtil;
import com.apexedu.util.StringUtil;
import com.apexedu.util.json.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * 门户服务资源组件
 * Created by ly on 2014/6/28.
 */
@Controller
public class ServerController extends BaseController {
    private static final String portalKey = ConfigUtil.getString("portalCode");//门户接入号

    @RequestMapping("/se/v")
    public
    @ResponseBody
    String getVersion() {
        return "1.0";
    }

    /**
     * 订阅服务初始界面
     *
     * @param portletid 服务组件id
     * @param request   --
     * @param map       --
     * @return 订阅服务初始界面
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_init, method = {RequestMethod.POST, RequestMethod.GET})
    public String init(@PathVariable(ActionConstant.Variable_Portlet_Id) String portletid, HttpServletRequest request,
                       ModelMap map) {
        TSysUser user = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
        String userid = user.getUserloginid();
        if (Constants.ProjectInfo.PROJECT_CODE.equals("shd")) {
            List <TMessage> ls_oa= getMessage(request,user,"unread","oa",5);
            List <TMessage> ls_hr= getMessage(request, user, "unread", "hr", 5);
            request.setAttribute("ls_oa", ls_oa);
            request.setAttribute("ls_hr", ls_hr);
        }
        log.debug("userid : " + userid + " , portletid " + portletid);
        map.put("userid", userid); //用户登录账号
        map.put("portletid", portletid); //服务组件id

        String sql = "select menuid,menuname from t_sys_menu where belongtoapp=?" +
                " and length(menuid)=9 and state=1 order by ordernum desc,menuid";
        //可订阅服务集合
        List<TSysMenu> listMenu = DBUtil.queryAllBeanList(sql, TSysMenu.class, portalKey);
        sql = "select menuid,menuname,sftj,menupath,upmenuid,tb,djcs,bgcolor,color from t_sys_menu m where belongtoapp=?" +
                "and exists (select null from t_sys_user a,t_sys_user_role b,t_sys_role c,t_sys_role_menu d" +
                " where a.userid=b.userid and b.roleid=c.roleid and  c.roleid=d.roleid and d.menuid=m.menuid  and c.state=1 and a.userloginid=?)" +
                " and length(menuid)=12 and state=1 order by upmenuid,ordernum desc,menuid";
        List<TSysMenu> listMenuSub = DBUtil.queryAllBeanList(sql, TSysMenu.class, portalKey, userid);
        List<TSysMenu> listMenuRemove = new ArrayList<TSysMenu>(); //删除无下级资源的资源分类
        for (TSysMenu menu : listMenu) {
            for (TSysMenu menuSub : listMenuSub) {
                if (menu.getMenuid().equals(menuSub.getUpmenuid())) {
                    menu.getListMenuSub().add(menuSub);
                }
            }
            if (menu.getListMenuSub().isEmpty()) {
                listMenuRemove.add(menu);
            }
        }
        listMenu.removeAll(listMenuRemove);
        map.put("listMenu", listMenu);

        //热门服务集合
        Collections.sort(listMenuSub, new Comparator<TSysMenu>() {
            @Override
            public int compare(TSysMenu o1, TSysMenu o2) {
                return o2.getDjcs().intValue() - o1.getDjcs().intValue();
            }
        });
        map.put("listMenuHot", listMenuSub);

        //已订阅服务集合
        sql = "select distinct dy.wid as dyid,a.menuid,a.menuname,a.sftj,a.menupath,a.upmenuid,a.tb,a.bgcolor,a.color " +
                " from t_sys_menu a inner join t_fw_dy dy on dy.menuid=a.menuid " +
                " inner join t_fw fw on fw.wid=dy.fwid " +
                " inner join t_sys_role_menu d on d.menuid=a.menuid" +
                " inner join t_sys_role c on c.roleid=d.roleid and c.state=1" +
                " inner join t_sys_user_role b on b.roleid=c.roleid" +
                " inner join t_sys_user u on  u.userid=b.userid and u.userloginid=?" +
                " where a.belongtoapp=? and length(a.menuid)=12 and a.state=1 " +
                " and (dy.userid=? or fw.fwlb in (1,3)) and fw.portletid=?" +
        " order by dy.wid";
        List<TSysMenu> listMenuDy = DBUtil.queryAllBeanList(sql, TSysMenu.class, userid, portalKey, userid, portletid);
        map.put("listMenuDy", listMenuDy);
        if (Constants.ProjectInfo.PROJECT_CODE.equals("shd")) {
            return ActionConstant.Server.Forward_init_shd;
        } else if (Constants.ProjectInfo.PROJECT_CODE.equals("shmy")) {
            return ActionConstant.Server.Forward_init_shmy;
        }  else if (Constants.ProjectInfo.PROJECT_CODE.equals("sdyz")) {
            return ActionConstant.Server.Forward_init_sdyz;
        }  else if (Constants.ProjectInfo.PROJECT_CODE.equals("szgz")) {
            return ActionConstant.Server.Forward_init_szgz;
        } else {
            return ActionConstant.Server.Forward_init;
        }

    }

    /**
     * 用户订阅服务资源
     *
     * @param menuid    服务资源id
     * @param portletid 服务组件id
     * @param request   --
     * @return 服务资源id
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_saveDy, method = RequestMethod.POST)
    @ResponseBody
    public String saveDy(@PathVariable(ActionConstant.Server.Variable_menuid) String menuid,
                         @PathVariable(ActionConstant.Variable_Portlet_Id) String portletid, HttpServletRequest request) {
        TSysUser tSysUser = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
        String userid = tSysUser.getUserloginid();
        String id = SeqFactory.getNewSequenceAlone();
        String sql = "insert into t_fw_dy(wid,fwid,userid,menuid) select ?,wid,?,? from t_fw fw" +
                " where portletid=? and not exists (select null from t_fw_dy where fwid=fw.wid and userid=? and menuid=?)";
        if (DBUtil.executeSQL(sql, id, userid, menuid, portletid, userid, menuid) > 0) {
            return id;
        }
        return null;
    }

    /**
     * 用户取消订阅服务资源
     *
     * @param id 服务资源id
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_delDy, method = RequestMethod.POST)
    @ResponseBody
    public void delDy(@PathVariable(ActionConstant.Variable_Id) String id) {
        //只有admin可删除导航组件资源和默认服务组件资源
        DBUtil.executeSQL("delete from t_fw_dy a where wid=?", id);
    }

    /**
     * 获取服务资源
     *
     * @param portletid 服务组件id
     * @param request   --
     * @return 获取服务资源
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_getFwzy, method = RequestMethod.GET)
    @ResponseBody
    public String getFwzj(@PathVariable(ActionConstant.Variable_Portlet_Id) String portletid, HttpServletRequest request) {
        TSysUser tSysUser = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
        String userid = tSysUser.getUserloginid();
        String sql = "select menuid,menuname from t_sys_menu where belongtoapp=?" +
                " and length(menuid)=9 and state=1 order by ordernum desc,menuid";
        //可订阅服务集合
        List<TSysMenu> listMenu = DBUtil.queryAllBeanList(sql, TSysMenu.class, portalKey);
        sql = "select menuid,menuname,sftj,menupath,upmenuid,tb,djcs from t_sys_menu a where a.belongtoapp=?" +
                " and exists (select null from t_fw_dy dy inner join t_fw fw on fw.wid=dy.fwid" +
                " where dy.menuid=a.menuid and fw.portletid=? and (fw.fwlb!='2' or dy.userid=?))" +
                " and exists (select null from t_sys_user u,t_sys_user_role b,t_sys_role c,t_sys_role_menu d" +
                " where u.userid=b.userid and b.roleid=c.roleid and  c.roleid=d.roleid and d.menuid=a.menuid  and c.state=1 and u.userloginid=?)" +
                " and length(menuid)=12 order by upmenuid,ordernum desc,menuid";
        List<TSysMenu> listMenuSub = DBUtil.queryAllBeanList(sql, TSysMenu.class, portalKey, portletid, userid, userid);
        List<TSysMenu> listMenuRemove = new ArrayList<TSysMenu>(); //删除无下级资源的资源分类
        for (TSysMenu menu : listMenu) {
            for (TSysMenu menuSub : listMenuSub) {
                if (menu.getMenuid().equals(menuSub.getUpmenuid())) {
                    menu.getListMenuSub().add(menuSub);
                }
            }
            if (menu.getListMenuSub().isEmpty()) {
                listMenuRemove.add(menu);
            }
        }
        listMenu.removeAll(listMenuRemove);
        return JsonUtil.getJSONString(listMenu);
    }

    /**
     * 更新资源的点击次数
     *
     * @param menuid 资源id
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_updateDjcs, method = RequestMethod.POST)
    @ResponseBody
    public void updateDjcs(@PathVariable(ActionConstant.Server.Variable_menuid) String menuid) {
        DBUtil.executeSQL("update t_sys_menu set djcs=nvl(djcs,0)+1 where menuid=?", menuid);
    }

    /**
     * 获取服务类别
     *
     * @param portletid 组件id
     * @return 服务类别
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_getFwLb, method = RequestMethod.GET)
    @ResponseBody
    public String getFwLb(@PathVariable(ActionConstant.Variable_Portlet_Id) String portletid) {
        return (String) DBUtil.queryFieldValue("select fwlb from t_fw where portletid=?", portletid);
    }

    /**
     * 更新服务类别
     *
     * @param portletid 组件id
     * @param fwlb      服务类别
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_saveFw, method = RequestMethod.POST)
    @ResponseBody
    public void saveFw(@PathVariable(ActionConstant.Variable_Portlet_Id) String portletid,
                       @PathVariable(ActionConstant.Server.Variable_fwlb) String fwlb) {
        if (DBUtil.count(" from t_fw where portletid = ?", portletid) > 0) {
            DBUtil.executeSQL("update t_fw set fwlb = ? where portletid = ?", fwlb, portletid);
        } else {
            DBUtil.executeSQL("insert into t_fw(wid,portletid,fwlb) values(?,?,?)",
                    SeqFactory.getNewSequenceAlone(), portletid, fwlb);
        }
    }

    /**
     * 获取移动端显示的服务
     *
     * @param userid
     * @return
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_getMobileFw, method = RequestMethod.GET)
    @ResponseBody
    public String getMobileFw(@PathVariable(ActionConstant.Variable_User_Id) String userid) {
        debug("<<{}>> getMobileFw start , userid : {}", this.getClass().getSimpleName(), userid);
        LittleTimer timer = new LittleTimer();
        try {
            String sql = "select menuid,menuname,sftj,menupath,upmenuid,tb,djcs from t_sys_menu a where a.belongtoapp=? and a.ismobile=1" +
                    " and exists (select null from t_sys_user u,t_sys_user_role b,t_sys_role c,t_sys_role_menu d" +
                    " where u.userid=b.userid and b.roleid=c.roleid and c.roleid=d.roleid and d.menuid=a.menuid and c.state=1 and u.userloginid=?)" +
                    " and length(menuid)=12 order by upmenuid,ordernum desc,menuid";
            List<TSysMenu> listMenuSub = DBUtil.queryAllBeanList(sql, TSysMenu.class, portalKey, userid);
            return JsonUtil.getJSONString(listMenuSub);
        } catch (Exception e) {
            error("<<{}>> getMobileFw error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> getMobileFw end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 服务详情界面
     *
     * @return
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_serverDetail, method = RequestMethod.GET)
    public String serverDetail(@PathVariable(ActionConstant.Server.Variable_menuid) String menuid, HttpServletRequest request) {
        debug("<<{}>> serverDetail start , menuid : {}", this.getClass().getSimpleName(), menuid);
        LittleTimer timer = new LittleTimer();
        try {
            TSysUser user = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
            if (Constants.ProjectInfo.PROJECT_CODE.equals("shd")) {
                List<TMessage> ls_oa = getMessage(request, user, "unread", "oa", 5);
                List<TMessage> ls_hr = getMessage(request, user, "unread", "hr", 5);
                request.setAttribute("ls_oa", ls_oa);
                request.setAttribute("ls_hr", ls_hr);
            }
            request.setAttribute("protypes", DBUtil.queryAllBeanList("select * from T_PROBLEMTYPE", TProblemType.class));//问题类型
            request.setAttribute("tsysmenu", getServiceMenu(menuid));//查询服务信息
            request.setAttribute("menuid", menuid);//服务id
            request.setAttribute("regus", getRegulations(menuid));//服务资源规则
            getProblomList(request, menuid);
            return ActionConstant.Server.Forward_serverDetail;
        } catch (Exception e) {
            error("<<{}>> serverDetail error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> serverDetail end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 服务信息
     *
     * @param menuid 服务id
     * @return
     */
    public TSysMenu getServiceMenu(String menuid) {
        debug("<<{}>> getServiceMenu start", this.getClass().getSimpleName());// 日志
        LittleTimer timer = new LittleTimer(); // 计时器
        try {
            TSysMenu t = DBUtil.queryBean("select * from t_sys_menu t where t.menuid = ?", TSysMenu.class, menuid);
            return t;
        } catch (Exception e) {
            error("<<{}>> getServiceMenu error : {}", this.getClass().getSimpleName(), e); // 日志
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> getServiceMenu end : {}", this.getClass().getSimpleName(), timer.end());// 计算方法执行时间
        }
    }
    /**
     * 服务咨询界面
     *
     * @return
     */
    @RequestMapping(value =ActionConstant.Server.RequestMapping_serverConsulting, method = RequestMethod.GET)
    public String serverConsulting(HttpServletRequest request) {
        debug("<<{}>> serverDetail start , menuid : {}", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        TSysUser user = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
        if (Constants.ProjectInfo.PROJECT_CODE.equals("shd")) {
            List<TMessage> ls_oa = getMessage(request, user, "unread", "oa", 5);
            List<TMessage> ls_hr = getMessage(request, user, "unread", "hr", 5);
            request.setAttribute("ls_oa", ls_oa);
            request.setAttribute("ls_hr", ls_hr);
        }
        String userid = user.getUserloginid();
        try {
            String sql = "select menuid,menuname from t_sys_menu where belongtoapp=?" +
                    " and length(menuid)=9 and state=1 order by ordernum desc,menuid";
            //可订阅服务集合
            List<TSysMenu> listMenu = DBUtil.queryAllBeanList(sql, TSysMenu.class, portalKey);
            sql = "select menuid,menuname,sftj,menupath,upmenuid,tb,djcs,bgcolor,color from t_sys_menu m where belongtoapp=?" +
                    "and exists (select null from t_sys_user a,t_sys_user_role b,t_sys_role c,t_sys_role_menu d" +
                    " where a.userid=b.userid and b.roleid=c.roleid and  c.roleid=d.roleid and d.menuid=m.menuid  and c.state=1 and a.userloginid=?)" +
                    " and length(menuid)=12 and state=1 order by upmenuid,ordernum desc,menuid";
            List<TSysMenu> listMenuSub = DBUtil.queryAllBeanList(sql, TSysMenu.class, portalKey, userid);
            List<TSysMenu> listMenuRemove = new ArrayList<TSysMenu>(); //删除无下级资源的资源分类
            for (TSysMenu menu : listMenu) {
                for (TSysMenu menuSub : listMenuSub) {
                    if (menu.getMenuid().equals(menuSub.getUpmenuid())) {
                        menu.getListMenuSub().add(menuSub);
                    }
                }
                if (menu.getListMenuSub().isEmpty()) {
                    listMenuRemove.add(menu);
                }
            }
            listMenu.removeAll(listMenuRemove);
            request.setAttribute("listMenu", listMenu);

            request.setAttribute("userid", userid);
            request.setAttribute("protypes", DBUtil.queryAllBeanList("select * from T_PROBLEMTYPE", TProblemType.class));//问题类型

            return ActionConstant.Server.Forward_serverConsulting;
        } catch (Exception e) {
            error("<<{}>> serverDetail error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> serverDetail end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 获取服务资源规则
     *
     * @param menuid 服务id
     * @return
     */
    public List<TRegulation> getRegulations(String menuid) {
        debug("<<{}>> getRegulations start", this.getClass().getSimpleName());// 日志
        LittleTimer timer = new LittleTimer(); // 计时器
        try {
            String sql = "select t.* from t_regulation t left join t_regulation_gx r on t.wid = r.gzwid where r.zywid = ?";
            List<TRegulation> lists = DBUtil.queryAllBeanList(sql, TRegulation.class, menuid);
            return lists;
        } catch (Exception e) {
            error("<<{}>> getRegulations error : {}", this.getClass().getSimpleName(), e); // 日志
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> getRegulations end : {}", this.getClass().getSimpleName(), timer.end());// 计算方法执行时间
        }
    }

    /**
     * 查询问题
     *
     * @param menuid 服务id
     */
    public void getProblomList(HttpServletRequest request, String menuid) {
        debug("<<{}>> getRegulations start", this.getClass().getSimpleName());// 日志
        LittleTimer timer = new LittleTimer(); // 计时器
        try {
            TSysUser user = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
            String userid = user.getUserloginid();
            String sql = "";
            List<TProblemNew> lists;
            if ("admin".equals(userid)) {
                sql = "select t.* from t_PROBLEM t where t.sid = ? order by t.ptime desc ";
                lists = DBUtil.queryAllBeanList(sql, TProblemNew.class, menuid);
            } else {
                //一般用户可以查看自己或启用的问题
                sql = "select t.* from t_PROBLEM t where （t.puser = ? or t.state=1) and t.sid = ? order by t.ptime desc";
                lists = DBUtil.queryAllBeanList(sql, TProblemNew.class, userid, menuid);
            }


            for (TProblemNew s : lists) {// 回复列表
                s.setAnspros(DBUtil.queryAllBeanList("select * from T_ANSWERPRO t  where t.pid = ? order by t.ptime desc", TAnswerPro.class, s.getWid()));
                s.setDiscusss(DBUtil.queryAllBeanList("select * from t_discuss t  where t.pid = ? order by t.dtime desc", TDiscuss.class, s.getWid()));
            }
            request.setAttribute("userid", userid);
            request.setAttribute("pros", lists);
        } catch (Exception e) {
            error("<<{}>> getProblomList error : {}", this.getClass().getSimpleName(), e); // 日志
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> getProblomList end : {}", this.getClass().getSimpleName(), timer.end());// 计算方法执行时间
        }
    }

    /**
     * 保存服务问题
     *
     * @param request
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_saveServiceProblem, method = RequestMethod.POST)
    @ResponseBody
    public void saveServiceProblem(HttpServletRequest request) throws IOException {
        debug("<<{}>> saveServiceProblem start", this.getClass().getSimpleName());// 日志
        //处理post中文问题
        request.setCharacterEncoding("UTF-8");
        LittleTimer timer = new LittleTimer(); // 计时器
        try {
            String prostr=request.getParameter("pro");
            TProblemNew pro = (TProblemNew) JsonUtil.getDTO(prostr, TProblemNew.class);
            //山大项目需要转换字符集
//            pro.setPproblem(new String(pro.getPproblem().getBytes("8859_1"), "UTF-8"));
//            pro.setPcontent(new String(pro.getPcontent().getBytes("8859_1"), "UTF-8"));
//            pro.setZymc(new String(pro.getZymc().getBytes("8859_1"), "UTF-8"));
            if (pro != null) {
                if (StringUtil.isBlank(pro.getWid())) {
                    String sql = "insert into t_problem(wid,pproblem,puser,ptime,pcontent,sid,zymc,tid) values (?,?,?,?,?,?,?,?)";
                    new DBUtil().executeSQL(sql, SeqFactory.getNewSequenceAlone(), pro.getPproblem(), pro.getPuser(), DateUtil.formatDate(new Date()),
                            pro.getPcontent(), pro.getSid(), pro.getZymc(),pro.getTid());
                }
            }
        } catch (Exception e) {
            error("<<{}>> saveServiceProblem error : {}", this.getClass().getSimpleName(), e); // 日志
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> saveServiceProblem end : {}", this.getClass().getSimpleName(), timer.end());// 计算方法执行时间
        }
    }

    /**
     * 保存服务问题的回复
     * @param request
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_saveServiceAns, method = RequestMethod.POST)
    @ResponseBody
    public void saveServiceAns(HttpServletRequest request) {
        debug("<<{}>> saveServiceAns start", this.getClass().getSimpleName());// 日志
        LittleTimer timer = new LittleTimer(); // 计时器
        try {
            TAnswerPro ans = (TAnswerPro) JsonUtil.getDTO(request.getParameter("ans"), TAnswerPro.class);
//            ans.setPcontent(new String(ans.getPcontent().getBytes("8859_1"), "UTF-8"));
            if (ans != null) {
                if (StringUtil.isBlank(ans.getWid())) {
                    String sql = "insert into T_ANSWERPRO(wid,puser,ptime,pcontent,pid) values (?,?,?,?,?)";
                    new DBUtil().executeSQL(sql, SeqFactory.getNewSequenceAlone(), ans.getPuser(), DateUtil.formatDate(new Date()), ans.getPcontent(),
                            ans.getPid());
                }
            }
        } catch (Exception e) {
            error("<<{}>> saveServiceAns error : {}", this.getClass().getSimpleName(), e); // 日志
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> saveServiceAns end : {}", this.getClass().getSimpleName(), timer.end());// 计算方法执行时间
        }
    }

    /**
     * 提交服务问题评论
     * @param request
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_saveServiceDis, method = RequestMethod.POST)
    @ResponseBody
    public void saveServiceDis(HttpServletRequest request) {
        debug("<<{}>> saveServiceDis start", this.getClass().getSimpleName());// 日志
        LittleTimer timer = new LittleTimer(); // 计时器
        try {
            TDiscuss dis = (TDiscuss) JsonUtil.getDTO(request.getParameter("dis"), TDiscuss.class);
//            dis.setDcontent(new String(dis.getDcontent().getBytes("8859_1"), "UTF-8"));
            if (dis != null) {
                if (StringUtil.isBlank(dis.getWid())) {
                    String sql = "insert into T_DISCUSS(wid,duser,dtime,dcontent,pid) values (?,?,?,?,?)";
                    new DBUtil().executeSQL(sql, SeqFactory.getNewSequenceAlone(), dis.getDuser(), DateUtil.formatDate(new Date()), dis.getDcontent(),
                            dis.getPid());
                }
            }
        } catch (Exception e) {
            error("<<{}>> saveServiceDis error : {}", this.getClass().getSimpleName(), e); // 日志
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> saveServiceDis end : {}", this.getClass().getSimpleName(), timer.end());// 计算方法执行时间
        }
    }

    /**
     * 删除问题
     * @param request
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_deletePro, method = RequestMethod.POST)
    @ResponseBody
    public void deletePro(HttpServletRequest request){
        debug("<<{}>> 删除提问的问题  start", this.getClass().getSimpleName());// 日志
        LittleTimer timer = new LittleTimer(); // 计时器
        try {
            String proid = request.getParameter("proid");
            DBUtil db = new DBUtil();
            db.executeSQL("delete t_problem t where t.wid = ?", proid);//删除问题
            db.executeSQL("delete T_ANSWERPRO t where t.pid = ?", proid);//删除回复
            db.executeSQL("delete T_DISCUSS t where t.pid = ?", proid);//删除评论
        } catch (Exception e) {
            error("<<{}>> 删除提问的问题  error : {}", this.getClass().getSimpleName(), e); // 日志
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> 删除提问的问题  end : {}", this.getClass().getSimpleName(), timer.end());// 计算方法执行时间
        }
    }


    /**
     * 删除评论
     * @param request
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_deleteDis, method = RequestMethod.POST)
    @ResponseBody
    public void deleteDis(HttpServletRequest request){
        debug("<<{}>> 删除服务资源问题评论  start", this.getClass().getSimpleName());// 日志
        LittleTimer timer = new LittleTimer(); // 计时器
        try {
            String proid = request.getParameter("proid");
            DBUtil db = new DBUtil();
            db.executeSQL("delete T_DISCUSS t where t.wid = ?", proid);//删除评论
        } catch (Exception e) {
            error("<<{}>> 删除服务资源问题评论  error : {}", this.getClass().getSimpleName(), e); // 日志
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> 删除服务资源问题评论  end : {}", this.getClass().getSimpleName(), timer.end());// 计算方法执行时间
        }
    }

    /**
     * 删除回复
     * @param request
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_deleteAns, method = RequestMethod.POST)
    @ResponseBody
    public void deleteAns(HttpServletRequest request){
        debug("<<{}>> 删除服务资源问题的回复  start", this.getClass().getSimpleName());// 日志
        LittleTimer timer = new LittleTimer(); // 计时器
        try {
            String proid = request.getParameter("proid");
            DBUtil db = new DBUtil();
            db.executeSQL("delete T_ANSWERPRO t where t.wid = ?", proid);//删除评论
        } catch (Exception e) {
            error("<<{}>> 删除服务资源问题的回复 error : {}", this.getClass().getSimpleName(), e); // 日志
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> 删除服务资源问题的回复  end : {}", this.getClass().getSimpleName(), timer.end());// 计算方法执行时间
        }
    }

    /**
     * 启用与禁用问题
     * @param request
     */
    @RequestMapping(value = ActionConstant.Server.RequestMapping_changestatePro, method = RequestMethod.POST)
    @ResponseBody
    public void changestatePro(HttpServletRequest request){
        debug("<<{}>> 启用禁用提问的问题  start", this.getClass().getSimpleName());// 日志
        LittleTimer timer = new LittleTimer(); // 计时器
        String state = request.getParameter("state");
        String proid = request.getParameter("proid");
        DBUtil db = new DBUtil();
        try {
            db.executeSQL("update t_problem t set t.state=? where t.wid = ?",state,proid);//启用禁用问题
        } catch (Exception e) {
            error("<<{}>> 启用禁用提问的问题  error : {}", this.getClass().getSimpleName(), e); // 日志
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> 启用禁用提问的问题  end : {}", this.getClass().getSimpleName(), timer.end());// 计算方法执行时间
        }
    }
}
